<template>
  <!--登录页面整体的盒子 -->
  <div class="login-container">
    <!-- 登录盒子 -->
    <div class="login-box">
      <!-- 标题盒子 -->
      <div class="title-box">
      </div>
      <!-- 表单区域 -->
      <!-- rules接收表单的验证方式 -->
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop='password'>
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 登录 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="width: 100%;">立即登录</el-button>
          <!-- 注册 -->
          <el-link type="info" @click="$router.push('/register')">去注册</el-link>
        </el-form-item>
      </el-form>
      <!--  -->
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/index'
import { mapMutations } from 'vuex'
export default {
  name: 'my-Login',
  data() {
    return {
      form: { // 表单的数据对象
        // 用户名
        username: '',
        // 密码
        password: ''
      },
      rules: { // 表单的验证规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // 正则表达式
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapMutations(['updataToken']),
    onSubmit() {
      // 1.判断是否通过校验
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 通过了校验
          const { data: res } = await loginAPI(this.form)

          // console.log(res)
          // 判断res.code是否等于1 等于抛出错误提示message
          if (res.code === 1) return this.$message.error(res.message)
          // 否则登录成功
          this.$message.success(res.message)
          // 保存token值
          // 1.现在vuex中 state创建token变量
          // 2.使用mutations：{updataToken(state,val){state。token=val}}保存taken
          // 3.导入mapMutations
          // 4.映射导入方法 ...mapMutations(['updataToken']),
          // 5.调用方法
          this.updataToken(res.token)
          this.$router.push('/')
          this.$store.dispatch('getUserINfoActions')
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100%;
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 40rem;
  height: 27rem;
  background-color: #fff;
  border-radius: 3px;
  box-sizing: border-box;
  padding: 0 3rem;

  .title-box {
    height: 6rem;
    background: url("../../assets/images/login_title.png") center no-repeat;
  }
}
</style>
